package com.zstu.mall.web.user;

import com.zstu.mall.entity.ProductClass;
import com.zstu.mall.service.ProductClassService;
import com.zstu.mall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Controller
public class IndexController {

    @Autowired
    private ProductClassService productClassService;

    @Autowired
    private ProductService productService;

    /**
     * 打开首页
     * @return
     */
    @RequestMapping("/index.html")
    public String toIndex(HttpServletRequest request, HttpServletResponse response) {
        //控制最大类型数
        Pageable pageable = new PageRequest(0, 20);
        List<ProductClass> productClasses = productClassService.findByStatus(pageable);
        pageable = new PageRequest(0, 3);
        for(ProductClass productClass : productClasses){
            productClass.setProducts(productService.findByType(productClass.getType(), pageable));
        }
        request.getSession().setAttribute("typelist",productClasses);
        System.out.println("session over");
        return "mall/index";
    }

    /**
     * 访问根目录转发到首页
     * @return
     */
    @RequestMapping("/")
    public String index(){
        return "forward:/index.html";
    }

}
